<template>
  <div>
    <div class="permissManaTool">
      <el-input placeholder="请输入角色的英文名" v-model="role.name">
        <template slot="prepend">ROLE_</template>
      </el-input>
      <el-input v-model="role.nameZh" size="small" placeholder="请输入角色的中文名"/>
      <el-button size="small" type="primary" icon="el-icon-plus" @click="doAddRole">添加角色</el-button>
    </div>

    <div class="permissManaMain">
      <!--  accordion 是否手风琴模式-->
      <!--  当前激活面板改变时触发(如果是手风琴模式，参数 activeNames 类型为string，否则为array)-->
      <el-collapse v-model="activeName" accordion @change="handleChange">
        <el-collapse-item :title="r.nameZh" :name="r.id" v-for="(r,index) in roles" :key="index">
          <el-card class="box-card">
            <div slot="header" class="clearfix">
              <span>可访问资源</span>
              <el-button style="float: right; padding: 3px 0;color: red" type="text" icon="el-icon-delete"
                         @click="doDeleteRole(r)"/>
            </div>
            <div>
              <el-tree show-checkbox
                       :data="allMenus"
                       :props="defaultProps"
                       ref="tree"
                       :key="index"
                       :default-checked-keys="selectedMenus"
                       node-key="id"></el-tree>
              <div style="display: flex;justify-content: flex-end">
                <el-button size="mini" @click="cancelUpdate">取消修改</el-button>
                <el-button size="mini" type="primary" @click="doUpdate(r.id,index)">确认修改</el-button>
              </div>
            </div>
          </el-card>
        </el-collapse-item>
      </el-collapse>
    </div>
  </div>
</template>

<script>
export default {
  name: "PermissMana",
  data() {
    return {
      // -1 折叠面板会折叠起来
      activeName: -1,
      role: {
        name: '',
        nameZh: ''
      },
      roles: [],
      // 绑定选中的菜单
      selectedMenus: [],
      allMenus: [],
      defaultProps: {
        // 绑定的子菜单属性名
        children: 'children',
        // 绑定要显示的属性
        label: 'name'
      }
    }
  },
  mounted() {
    this.initRoles();
  },
  methods: {
    doAddRole() {
      if (!this.role.name || !this.role.nameZh) {
        this.$message({
          type: 'warning',
          message: '角色英文名和中文名不能为空'
        });
        return;
      }
      this.postRequest('/system/basic/permiss/', this.role).then(resp => {
        if (resp) {
          this.initRoles();
          this.activeName = -1;
        }
      })
    },
    initRoles() {
      this.getRequest('/system/basic/permiss/').then(resp => {
        if (resp) {
          this.roles = resp;
        }
      })
    },
    // 折叠面板选中的角色改变时触发
    handleChange(rid) {
      if (rid) {
        this.initMenus();
        this.initSelectedMenus(rid);
      }
    },
    // 初始化所有的菜单
    initMenus() {
      this.getRequest('/system/basic/permiss/menus').then(resp => {
        if (resp) {
          this.allMenus = resp;
        }
      })
    },
    // 根据角色查找对应的菜单
    initSelectedMenus(rid) {
      this.getRequest('/system/basic/permiss/mid/' + rid).then(resp => {
        if (resp) {
          this.selectedMenus = resp;
        }
      });
    },
    // 删除当前角色
    doDeleteRole(role) {
      this.$confirm('此操作将永久删除[' + role.nameZh + ']角色, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteRequest('/system/basic/permiss/role/' + role.id).then(resp => {
          if (resp) {
            this.initRoles();
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    cancelUpdate() {
      this.activeName = -1;
    },
    doUpdate(rid, index) {
      let tree = this.$refs.tree[index];
      let selectedKeys = tree.getCheckedKeys(true);
      let url = '/system/basic/permiss/?rid=' + rid;
      selectedKeys.forEach(key => {
        url += '&mids=' + key;
      });
      this.putRequest(url).then(resp => {
        if (resp) {
          this.activeName = -1;
        }
      })
    },
  }
}
</script>

<style scoped>
.permissManaTool {
  display: flex;
  justify-content: flex-start;
}

.permissManaTool .el-input {
  width: 300px;
  margin-right: 10px;
}

.permissManaMain {
  margin-top: 10px;
  width: 720px;
}
</style>
